<?php
namespace Users\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class AdminCombinationsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }	
	public function getadmincountries($userid){
		$select = $this->tableGateway->getSql()->select();
		$select->join('country', 'admin_combinations.country_id=country.country_id',array('countryNicename'=>'nicename'),'left');		
		$select->where('user_id="'.$userid.'"');
		$select->group('admin_combinations.country_id');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;		
	}
	public function checkexistingCombs( $combinations,$hid_user_id )
    {
		$combVals = explode( "#",$combinations );
		$select = $this->tableGateway->getSql()->select();			
		$select->where( 'industry_id='.$combVals[0] );
		$select->where( 'country_id='.$combVals[1] );
		if( $hid_user_id != "" )
		{
			$select->where( 'user_id!='. $hid_user_id );
		}
		$resultSet = $this->tableGateway->selectWith($select);
		$existingCount = $resultSet->count();
		return $existingCount;
		
	}

	public function getCombsCount( $user_id )
    {
		$select = $this->tableGateway->getSql()->select();			
		$select->where( 'user_id='. $user_id );
		$resultSet = $this->tableGateway->selectWith($select);
		$existingCombsCount = $resultSet->count();
		return $existingCombsCount;
	}

	public function loadCombinations( $user_id )
    {
		$select = $this->tableGateway->getSql()->select();			
		$select->join('country', 'admin_combinations.country_id=country.country_id',array('countryNicename'=>'nicename'),'left');
		$select->join('industry', 'admin_combinations.industry_id=industry.industry_id',array('industryNicename'=>'industryname'),'left');
		$select->where( 'user_id='. $user_id );
		$resultSet = $this->tableGateway->selectWith($select);
		
		$loadedCombinations = "";
		foreach( $resultSet as $resultRow )
		{
			$loadedCombinations .= $resultRow->industry_id . "/" . $resultRow->industryNicename . "#" . $resultRow->country_id . "-" . $resultRow->countryNicename . "!";
		}
		$loadedCombinations = substr( $loadedCombinations,0,(strlen($loadedCombinations)-1) ); 
		// echo "<pre>";print_r( $loadedCombinations );exit;
		return $loadedCombinations;
	}

	public function addCombinations( $user_id,$sessionCombinations )
    {
		$deleteStatus=$this->tableGateway->delete(array('user_id' => $user_id));
		if( $deleteStatus >= 0 )
		{
			$existingCombsCount = count( $sessionCombinations );
			
			for( $indCtryId = 0; $indCtryId < $existingCombsCount; $indCtryId++ )
			{
				foreach( $sessionCombinations[$indCtryId] as $industryInfo=>$countryInfo )
				{
					$country_id=explode("-",$countryInfo);
					$industry_id=explode( "/",$industryInfo );
					$data = array(
						'user_id' 	  	       => $user_id, 	
						'country_id' 		   => $country_id[0],
						'industry_id' 		   => $industry_id[0],
						'add_combination_date' 	=> date('Y-m-d H:i:s'),
						'last_combination_date' => date('Y-m-d H:i:s'),
					);	
					$insertresult=$this->tableGateway->insert($data);
				}
			}
			return $insertresult;
		}
	}

	public function deleteCombinations( $user_id )
    {
		$deleteStatus=$this->tableGateway->delete(array('user_id' => $user_id));
		return $deleteStatus;
	}

	public function getindustries($user_id,$type=''){
		$select = $this->tableGateway->getSql()->select();		
		$select->join('country', 'admin_combinations.country_id=country.country_id',array('countryNicename'=>'nicename'),'left');
		$select->join('industry', 'admin_combinations.industry_id=industry.industry_id',array('industryNicename'=>'industryname'),'left');
		$select->where( 'user_id='. $user_id );
		if($type=='country'){
			$select->group('admin_combinations.country_id');
		}
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}

	public function getAdminIndustries( $admin_id,$selectedCountry ){
		$select = $this->tableGateway->getSql()->select();		
		$select->join('industry', 'admin_combinations.industry_id=industry.industry_id',array('industryNicename'=>'industryname'),'left');
		$select->where( 'admin_combinations.user_id='. $admin_id );
		$select->where( 'admin_combinations.country_id='. $selectedCountry );
		$select->group('admin_combinations.industry_id');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function updateAdmin($countryId,$industryId,$toAdminId){
		$data = array(
				'user_id'       =>$toAdminId,
				);
		$result=$this->tableGateway->update($data, array('country_id' => $countryId,'industry_id' =>$industryId));
		return 	$result;			
	}
	public function combinationDetails($combinationId){
		$select = $this->tableGateway->getSql()->select();		
		$select->where( 'admin_combinations.combination_id='. $combinationId );
		$resultSet = $this->tableGateway->selectWith($select);
		return 	$resultSet->current();			
	}
	public function getallcombinations($admin_userId){
		$select = $this->tableGateway->getSql()->select();		
		$select->join('country', 'admin_combinations.country_id=country.country_id',array('countryNicename'=>'nicename'),'left');
		$select->join('industry', 'admin_combinations.industry_id=industry.industry_id',array('industryNicename'=>'industryname'),'left');
		$select->where( 'admin_combinations.user_id='. $admin_userId );
		$resultSet = $this->tableGateway->selectWith($select);
		return 	$resultSet;
	}
}